﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using CompanyX.Insurance.La.DataContracts;
using CompanyX.Shared.Utilities;
using System.Xml;

namespace Client
{
    public partial class Form1 : Form
    {
        private TypeCodeComboBox<OLI_LU_LINEBUS> cbLineOfBusiness;
        private TypeCodeComboBox<OLI_LU_POLPROD> cbPolicyProduct;
        private TypeCodeComboBox<OLI_LU_STATE> cbState;

        public Form1()
        {
            InitializeComponent();

            // Set values into combo boxes

            cbLineOfBusiness = new TypeCodeComboBox<OLI_LU_LINEBUS>();
            cbPolicyProduct = new TypeCodeComboBox<OLI_LU_POLPROD>();
            cbState = new TypeCodeComboBox<OLI_LU_STATE>();

            cbLineOfBusiness.ReplacePlainComboBox(ref this.comboBox2);
            cbPolicyProduct.ReplacePlainComboBox(ref this.comboBox3);
            cbState.ReplacePlainComboBox (ref this.comboBox4);

            this.comboBox1.SelectedIndex = 0;
            this.cbLineOfBusiness.SelectedIndex = 0;
            this.cbPolicyProduct.SelectedIndex = 0;
            this.cbState.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Instantiate WCF proxy

            CompanyX.Insurance.La.ServiceAgents.ProducerLicensingAndAppointmentsClient cli = new CompanyX.Insurance.La.ServiceAgents.ProducerLicensingAndAppointmentsClient();

             // Create and fill TxLifeRequest object for sending

            TXLifeRequest rq = new TXLifeRequest();
            rq.TransRefGUID = System.Guid.NewGuid().ToString();
            //rq.TransType.tc = (int)OLI_LU_TRANS_TYPE_CODES_TC._228;
            rq.TransType.Set_OLI_TRANS_PRODINQ();
            
            // Allow the user to choose supported or unsupported tx, to view error handling
            if (rbSupportedTX.Checked == true)
            {
                rq.TransSubType.tc = (int)TRANS_SUBTYPE_CODES_TC._22810;
            }
            else
            {
                rq.TransSubType.tc = (int)TRANS_SUBTYPE_CODES_TC._1;
            }

            // The party contains the proposed agent's full name, and
            // additional information to identify agent. User checking a box
            // allows us to omit this to check error handling

            if (cbOmitParty.Checked == false)
            {
                Party party = new Party();
                rq.OLifE.Party.Add(party);
                party.FullName = textBox1.Text;
                party.Producer.CarrierAppointment.Add(new CarrierAppointment());
                party.Producer.CarrierAppointment[0].CompanyProducerID = textBox2.Text;
                party.Producer.CarrierAppointment[0].CarrierCode = comboBox1.SelectedItem.ToString();
            }

            // The Holding contains information about the policy that
            // the agent is proposing to sell. Allow user to omit it to 
            // demonstrate error handling. 

            if (cbOmitHolding.Checked == false)
            {
                Holding holding = new Holding();
                rq.OLifE.Holding.Add(holding);
                holding.Policy.ProductType.tc = cbPolicyProduct.SelectedElementWithText.tc;
                holding.Policy.ProductType.Value = cbPolicyProduct.SelectedElementWithText.Value;
                holding.Policy.LineOfBusiness.tc = cbLineOfBusiness.SelectedElementWithText.tc;
                holding.Policy.LineOfBusiness.Value = cbLineOfBusiness.SelectedElementWithText.Value;
                holding.Policy.Life.FaceAmt = Convert.ToDecimal(textBox3.Text);
                holding.Policy.ApplicationInfo.ApplicationJurisdiction.tc = cbState.SelectedElementWithText.tc;
                holding.Policy.ApplicationInfo.ApplicationJurisdiction.Value = cbState.SelectedElementWithText.Value;
            }

            PolicyProduct p = new PolicyProduct();
            AdminTransactionProduct a = new AdminTransactionProduct();
            QualifiedPlanEntity q = new QualifiedPlanEntity();
            EntityType e1 = new EntityType();
            e1.PartyTypeCode.Set_OLI_PT_ORG();
            q.EntityType.Add(e1);
            a.QualifiedPlanEntity.Add(q);
            p.AdminTransactionProduct.Add(a);
            rq.OLifE.PolicyProduct.Add(p);

            InvestProduct ip = new InvestProduct();
            InformationService iss = new InformationService();
            iss.InformationServiceProvider.Set_OLI_SERVICE_LIPPER();
            ip.InformationService.Add(iss);
            rq.OLifE.InvestProduct.Add(ip);

            Criteria c = new Criteria();
            c.Operation.Set_LOGICAL_OPERATOR_WILDCARDMATCH();
            rq.CriteriaExpression.Criteria.Add(c);

            // test out the serialization/deserialization
            string xml = SerializationManager.Serialize(rq, false);
            //FileStream fs = new FileStream("C:\\temp\\1122req.xml", FileMode.Open, FileAccess.Read);
            //StreamReader sr = new StreamReader(fs);
            //string xml = sr.ReadToEnd();
            //TXLife txlife = SerializationManager.Deserialize<TXLife>(xml, false, CompanyX.Insurance.La.DataContracts.Declarations.SchemaVersion);

            TXLifeRequest rq2 = SerializationManager.Deserialize<TXLifeRequest>(xml, false, CompanyX.Insurance.La.DataContracts.Declarations.SchemaVersion);

            // Send it to the webservice and get response
            TXLifeResponse rs =  cli.GetProducerLicensingDetails(rq2);
 
            // Fetch data out of response and display to user
            label8.Text = "TransResult.ResultCode.tc = " + rs.TransResult.ResultCode.tc + " Value= "
                + GetGoodString(rs.TransResult.ResultCode.Value);

            string result = null;
            foreach (ResultInfo ri in rs.TransResult.ResultInfo)
            {
                result += "ResultInfo tc = " + ri.ResultInfoCode.tc + " Value= " +
                    GetGoodString(ri.ResultInfoCode.Value + "\n") ;
            }

            label9.Text = result;
        }
     

        private string GetGoodString (object o)
        {
            if (o == null)
                return "(null)" ;
            else if (o is string)
                return (string) o ;
            else return o.ToString () ;
        }
    }
}
